package cn.itsource.hrm.service.impl;

import cn.itsource.hrm.doc.CourseDoc;
import cn.itsource.hrm.domain.Course;
import cn.itsource.hrm.domain.CourseDetail;
import cn.itsource.hrm.domain.CourseMarket;
import cn.itsource.hrm.domain.CourseType;
import cn.itsource.hrm.dto.CourseDto;
import cn.itsource.hrm.feign.CourseFeign;
import cn.itsource.hrm.feign.DeleteFeign;
import cn.itsource.hrm.mapper.CourseDetailMapper;
import cn.itsource.hrm.mapper.CourseMapper;
import cn.itsource.hrm.mapper.CourseMarketMapper;
import cn.itsource.hrm.mapper.CourseTypeMapper;
import cn.itsource.hrm.service.ICourseService;
import cn.itsource.hrm.to.EmailSendTo;
import cn.itsource.hrm.to.SMSSendTo;
import cn.itsource.hrm.to.SystemMessageSendTo;
import cn.itsource.hrm.util.AjaxResult;
import cn.itsource.hrm.util.ValidUtils;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

import static cn.itsource.hrm.constants.MQConstants.*;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author xqf
 * @since 2021-01-14
 */
@Service
@Transactional
public class CourseServiceImpl extends ServiceImpl<CourseMapper, Course> implements ICourseService {
    @Autowired
    private CourseDetailMapper courseDetailMapper;
    @Autowired
    private CourseMarketMapper courseMarketMapper;
    @Autowired
    private CourseTypeMapper courseTypeMapper;
    @Autowired
    private CourseFeign courseFeign;
    @Autowired
    private DeleteFeign deleteFeign;

    @Autowired
    private RabbitTemplate rabbitTemplate;

    @Override
    public void insert(CourseDto courseDto) {
        //1.取出数据
        Course course = courseDto.getCourse();
        CourseDetail courseDetail = courseDto.getCourseDetail();
        CourseMarket courseMarket = courseDto.getCourseMarket();
        //2.校验参数
        //3.保存课程
        course.setStatus(Course.OFFLINE);
        course.setTenantId(26L);
        course.setTenantName("源码时代666");
        course.setUserId(42L);
        course.setUserName("yhptest6969");
        baseMapper.insert(course);
        //4.保存课程详情
        courseDetail.setId(course.getId());
        courseDetailMapper.insert(courseDetail);
        //5.保存课程营销
        courseMarket.setId(course.getId());
        courseMarketMapper.insert(courseMarket);
        //6.保存课程数量+1
        CourseType courseType = courseTypeMapper.selectById(course.getCourseTypeId());
        courseType.setTotalCount(courseType.getTotalCount()+1);
        courseTypeMapper.updateById(courseType);
    }

    /**
     * 课程上线
     * @param id
     */
    @Override
    public void onLineCourse(Long id) {
        //1.校验参数
        ValidUtils.isNotNull(id, "id格式不正确");
        Course course = baseMapper.selectById(id);
        ValidUtils.isNotNull(course,"课程不存在" );
        ValidUtils.isEquals(course.getStatus(), Course.OFFLINE, "课程已经上线啦");
        //2.修改课程上下线状态
        course.setStatus(Course.ONLINE);
        //3.修改数据库
        baseMapper.updateById(course);
        //4.构建doc对象
        CourseDoc courseDoc = new CourseDoc();
        CourseMarket courseMarket = courseMarketMapper.selectById(course.getId());
        BeanUtils.copyProperties(course,courseDoc );
        BeanUtils.copyProperties(courseMarket, courseDoc);
        courseDoc.setOnlineTime(new Date());
        //5.调用feign接口保存到es中
        AjaxResult ajaxResult = courseFeign.save(courseDoc);
        ValidUtils.isTrue(ajaxResult.isSuccess(),"保存失败");

        //发送课程消息
        sendMessage(course);
    }
    //发送课程消息
    private void sendMessage(Course course){
        //发送短信消息
        List<String> phones = Arrays.asList("18244229555","18244229556");
        String smsContextTemplate = "尊敬的用户您好，我们新的课程[ %s ]发布啦，快上车，没时间解释了，" +
                "<a href='http://itsource.cn/course/detail/%s'>戳我戳我戳我</a>";
        String smsContext = String.format(smsContextTemplate,course.getName(),course.getId() );
        rabbitTemplate.convertAndSend(EXCHANGE_NAME_DIRECT,ROUTINGKEY_MESSAGE_SMS,new SMSSendTo(phones,smsContext));

        //发送邮件消息
        List<String> emails = Arrays.asList("182442@qq.com","18244@qq.com");
        String emailContextTemplate = "尊敬的用户您好，我们新的课程[ %s ]发布啦，快上车，没时间解释了，" +
                "<a href='http://itsource.cn/course/detail/%s'>戳我戳我戳我</a>";
        String emailContext = String.format(emailContextTemplate,course.getName(),course.getId() );

        rabbitTemplate.convertAndSend(EXCHANGE_NAME_DIRECT,ROUTINGKEY_MESSAGE_EMAIL,new EmailSendTo(emails,emailContext));

        //发送站内信
        List<Long> ids = Arrays.asList(1L,2L);
        String title = "新课程发布啦";
        String messageContextTemplate = "尊敬的用户您好，我们新的课程[ %s ]发布啦，快上车，没时间解释了，" +
                "<a href='http://itsource.cn/course/detail/%s'>戳我戳我戳我</a>";
        String messageContext = String.format(messageContextTemplate,course.getName(),course.getId() );

        rabbitTemplate.convertAndSend(EXCHANGE_NAME_DIRECT,ROUTINGKEY_MESSAGE_SYSTEM,
                new SystemMessageSendTo(ids,title,messageContext));
    }
    /**
     * 课程下线
     * @param id
     */
    @Override
    public void offLineCourse(Long id) {
        //1.校验参数
        ValidUtils.isNotNull(id, "id格式不正确");
        Course course = baseMapper.selectById(id);
        ValidUtils.isNotNull(course,"课程不存在" );
        ValidUtils.isEquals(course.getStatus(), Course.ONLINE, "课程已经下线啦");
        //2.修改课程上下线状态
        course.setStatus(Course.OFFLINE);
        //3.修改数据库
        baseMapper.updateById(course);
        //5.调用feign接口删除es中数据
        CourseDoc courseDoc = new CourseDoc();
        courseDoc.setId(course.getId());
        AjaxResult ajaxResult = deleteFeign.delete(courseDoc);
        ValidUtils.isTrue(ajaxResult.isSuccess(),"删除失败");
    }
}
